package com.cqxyd.smartsite.service.impl;

import com.cqxyd.smartsite.dao.DeviceInfoMapper;
import com.cqxyd.smartsite.pojo.DeviceInfo;
import com.cqxyd.smartsite.pojo.DeviceInfoVo;
import com.cqxyd.smartsite.service.DeviceInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author Angus Yu
 * @date 2020-05-16 12:18
 */
@Service
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public PageInfo<DeviceInfoVo> getDeviceInfoListByPage(Integer pageNum, Integer pageSize, Integer programId, String seriaNo, String registNo, String typeId) {

        List<DeviceInfoVo> sysConfigList = deviceInfoMapper.selectDeviceInfo(programId, seriaNo, registNo, typeId);
        PageInfo<DeviceInfoVo> pageInfo = new PageInfo<DeviceInfoVo>(sysConfigList);
        return pageInfo;
    }

    @Override
    public DeviceInfo findDeviceInfo(Integer deviceId) {
        return deviceInfoMapper.selectByPrimaryKey(deviceId);
    }

    @Override
    public int addDeviceInfo(DeviceInfo deviceInfo) {
        return deviceInfoMapper.insert(deviceInfo);
    }

    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo) {
        return deviceInfoMapper.updateByPrimaryKeySelective(deviceInfo);
    }

    @Override
    public int deleteDeviceInfo(Integer deviceId) {

        return deviceInfoMapper.deleteByPrimaryKey(deviceId);
    }

    @Override
    public int updateLastHeartbeatTime(String serialNo, Date lastHeartbeatTime) {
        DeviceInfo deviceInfoSearch = new DeviceInfo();
        deviceInfoSearch.setSerialNo(serialNo);
        DeviceInfo deviceInfo = deviceInfoMapper.selectOne(deviceInfoSearch);
        if (deviceInfo == null) {
            return 0;
        }
        deviceInfo.setLastHeartbeatTime(lastHeartbeatTime);
        return deviceInfoMapper.updateByPrimaryKey(deviceInfo);
    }
}
